#ifndef FOOD_MANAGER_H
#define FOOD_MANAGER_H

#include "Food.h"
#include "Snake.h"
#include <vector>
#include <random>
#include <algorithm>

class FoodManager
{
private:
    std::vector<std::unique_ptr<Food>> foods; // 食物数组
    Container *parentContainer;               // 父容器
    int maxFoodCount;                         // 最大食物数量
    int foodSize;                             // 食物大小
    int Probability = 10;

    // 随机数生成器
    std::random_device rd;
    std::mt19937 gen;
    std::uniform_int_distribution<> disType;

    // 添加禁止区域定义
    static const int FORBIDDEN_X_MIN = 0;
    static const int FORBIDDEN_X_MAX = 50;
    static const int FORBIDDEN_Y_MIN = 0;
    static const int FORBIDDEN_Y_MAX = -80;

public:
    FoodManager(Container *parent, int maxCount = 5, int size = 30);
    ~FoodManager();

    // 食物生成逻辑
    bool generateFood(const Snake &snake, FoodType type = FOOD_NORMAL);
    bool generateRandomFood(const Snake &snake);

    bool generateFoodIfNeeded(const Snake &snake);

    // 碰撞检测
    Food *checkCollision(const Snake &snake);
    void handleCollision(Food *food, Snake *snake);

    // 食物管理
    void removeFood(Food *food);
    void clearAllFood();
    void updateFoods(const Snake &snake); // 定期更新食物    

    // 获取食物信息
    size_t getFoodCount() const { return foods.size(); }
    const std::vector<std::unique_ptr<Food>> &getFoods() const { return foods; }

private:
    // 位置验证
    bool isValidPosition(int x, int y, const Snake &snake);
    bool isPositionSafe(int x, int y, const Snake &snake, int safeDistance = 5);
    void getRandomPosition(int &x, int &y);
};

#endif